#!/bin/bash

extIn="flac"
extOut="flac"

function conv {
	echo "I'm in directory \"$1\""
	dirs=`ls -ld -C1 */`
	for dir in ${dirs} 
	do	
		cd "${dir}"
		conv "${dir}"
		cd ..
	done

	mkdir out

	files=`ls -C1 *.${extIn}`
	for file in $files
	do
		fileName=${file%.${extIn}}
		ffmpeg -i ${fileName}.${extIn} ./out/${fileName}.${extOut}
		if [ -f ./out/${fileName}.${extOut} ]; then
			rm ${fileName}.${extIn}
			mv ./out/${fileName}.${extOut} ${fileName}.${extOut}
			echo "Source file ${fileName}.${extIn} has been removed"
		fi
	done
	rm -r ./out
}

IFS=$'\n'

conv "`pwd`"

echo "DONE !"
